<?php
namespace app\filemanage\validate;

use think\Validate;
use think\DB;


class Login extends Validate
{
	protected $rule = [
		'username'   => 'require|alphaNum',
		'password'   => 'require',
		'repassword' => 'require|confirm:password',
		'captcha'    => 'require|captcha',
	]; 

	protected $message  =   [
        'username.require'   => '请输入用户名',
        'username.alphaNum'  => '用户名必须为数字或字母的组合',
        'username.length'    => '用户名长度必须在6-18位之间',
        'password.require'   => '请输入密码',
        'password.length'    => '密码长度必须在6-18位之间',
        'repassword.require' => '请再次输入密码',
        'repassword.confirm' => '两次输入的密码不同',
        'captcha.require'    => '请输入验证码',
        'captcha.captcha'    => '验证码错误',
    ];

    protected $scene = [
    	'login'    => ['username', 'password', 'captcha'],
    	'register' => ['username' => 'require|alphaNum|length:6,18|checkHasValue:username', 'password', 'repassword', 'captcha'],
    ];

    protected function checkHasValue($value, $rule) {
    	switch ($rule) {
    		case 'username':
    			$hasValue = Db::name('user')->where('username',$value)->find();
    			if (empty($hasValue)) {
					return true;
				} else {
					return '用户名已存在';
				}
    			break;
    		
    		default:
    			# code...
    			break;
    	}
    }
}


